package com.hz.util;

import java.util.*;
import java.sql.*;
import com.google.gson.Gson;
import com.hz.dbconn.DBConnect;

public class Autocomplete {
    /*
     * 实现自动提示功能，用于站内信收件人的填写，返回Json（jsonString）
     */
    @SuppressWarnings("unchecked")
    public static String Auto(String term){
    DBConnect dbc = new DBConnect();
    String jsonStr = "";
    String sql="";
    try {
  	 sql = "select id,realname,stuNum from user where realnamepy like '%"+term+"%' ";
  	 dbc.prepareStatement(sql);
  	 System.out.print(sql);
  	 ResultSet rs = dbc.executeQuery();
  	 List rsList = new ArrayList();
  	 Map map = null;
  	 while(rs.next()){
  		map = new HashMap();
  		map.put("id",rs.getInt(1));
  		map.put("name",rs.getString(2));
  		map.put("stunum",rs.getString(3));
  		rsList.add(map);
  	}
  	rs.close();
  	rs = null;
  	Gson gson = new Gson();
  	jsonStr = gson.toJson(rsList);
  	System.out.println("jsonStr="+jsonStr);
  } catch (SQLException e) {
  	// TODO Auto-generated catch block
  	e.printStackTrace();
  }finally{
  	dbc.close();
  	
  }
  return jsonStr;
    }

}
